/*
 *  UI_Menu.cpp
 *
 *	Date: 2011-8-8
 *  Author: linshenqi
 */

#include "stdafx.h"
#include "./UI_Frame.h"
#include "Resource.h"
#include "./SSCOM.h"

extern CDC *g_pDC;
extern CDC g_MemDC;

extern SSCOM theSSCOM;

UI_Menu::UI_Menu(const CRect *pRect, const GRAPHICPROP *pProp)
	: GraphicBase(pRect, pProp)
{
	m_Str[0] = "File";
	m_Str[1] = "Connection";
	m_Str[2] = "View";
	m_Str[3] = "Help";

	m_font[0] = COLOR_FONT_OUT;
	m_font[1] = COLOR_FONT_OUT;
	m_font[2] = COLOR_FONT_OUT;
	m_font[3] = COLOR_FONT_OUT;

	m_ID[0] = IDR_MENU_FILE;
	m_ID[1] = IDR_MENU_CONN;
	m_ID[2] = IDR_MENU_VIEW;
	m_ID[3] = IDR_MENU_HELP;

	m_lf.lfCharSet = DEFAULT_CHARSET;
	m_lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
	m_lf.lfEscapement = 0;
	strcpy(m_lf.lfFaceName, "Arial");
	m_lf.lfHeight = 24;
	m_lf.lfItalic = false;
	m_lf.lfOrientation = 0;
	m_lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
	m_lf.lfPitchAndFamily = DEFAULT_PITCH;
	m_lf.lfQuality = DEFAULT_QUALITY;
	m_lf.lfStrikeOut = false;
	m_lf.lfUnderline = false;
	m_lf.lfWidth = 0;
	m_lf.lfWeight = 0;
}

bool UI_Menu::OnPaint(CPaintDC *pDC, CDC *pMemDC)
{
	for(int i = 0; i < (sizeof(m_Str) / sizeof(m_Str[0])); ++i)
	{
		this->DrawText(&m_lf, &m_rc[i], m_Str[i], m_font[i], pDC, pDC);
	}

	return GraphicBase::OnPaint(pDC, pMemDC);
}

void UI_Menu::Redraw()
{
	for(int i = 0; i < (sizeof(m_Str) / sizeof(m_Str[0])); ++i)
	{
		this->DrawText(&m_lf, &m_rc[i], m_Str[i], m_font[i], g_pDC, g_pDC, REDRAW);
	}
}

bool UI_Menu::OnMouseMove(UINT nFlags, CPoint point)
{
	if(!this->m_pRect->PtInRect(point))
	{
		for(int i = 0; i < (sizeof(m_Str) / sizeof(m_Str[0])); ++i)
		{
			m_font[i] = COLOR_FONT_OUT;
			this->DrawText(&m_lf, &m_rc[i], m_Str[i], m_font[i], g_pDC, g_pDC);
		}
		return false;
	}

	for(int i = 0; i < (sizeof(m_Str) / sizeof(m_Str[0])); ++i)
	{
		if(m_rc[i].PtInRect(point))
		{
			m_font[i] = COLOR_FONT_IN;
		}
		else if(!m_rc[i].PtInRect(point))
		{
			m_font[i] = COLOR_FONT_OUT;
		}
		this->DrawText(&m_lf, &m_rc[i], m_Str[i], m_font[i], g_pDC, g_pDC);
	}
	return GraphicBase::OnMouseMove(nFlags, point);
}

bool UI_Menu::OnLButtonDown(UINT nFlags, CPoint point)
{
	CMenu *pop;
	CRect trc;
	for(int i = 0; i < (sizeof(m_Str) / sizeof(m_Str[0])); ++i)
	{
		if(m_rc[i].PtInRect(point))
		{
			this->m_menu.LoadMenu(m_ID[i]);
			pop = this->m_menu.GetSubMenu(0);
			if(i == 1)
			{
				if(::theSSCOM.GetStmt())
				{
					pop->CheckMenuItem(ID_CONN_CONNECTION, MF_CHECKED);
					pop->CheckMenuItem(ID_CONN_DISCONNECTION, MF_UNCHECKED);
				}
				else
				{
					pop->CheckMenuItem(ID_CONN_CONNECTION, MF_UNCHECKED);
					pop->CheckMenuItem(ID_CONN_DISCONNECTION, MF_CHECKED);
				}
			}
			

			if(i == 2)
			{
				UI_TitleBar *pp = (UI_TitleBar *)this->m_pParent;
				if(pp->m_Tab.m_nSel == 0)
				{
					pop->CheckMenuItem(ID_VIEW_SCENE01, MF_CHECKED);
					pop->CheckMenuItem(ID_VIEW_SCENE02, MF_UNCHECKED);
				}
				else
				{
					pop->CheckMenuItem(ID_VIEW_SCENE01, MF_UNCHECKED);
					pop->CheckMenuItem(ID_VIEW_SCENE02, MF_CHECKED);
				}
			}
			trc = m_rc[i];
			this->m_pWnd->ClientToScreen(&trc);
			pop->TrackPopupMenu(TPM_TOPALIGN | TPM_LEFTALIGN, trc.left, trc.bottom + 8, this->m_pWnd);
			break;
		}
	}

	return GraphicBase::OnLButtonDown(nFlags, point);
}

void UI_Menu::SetRect(const CRect *pRect)
{
	GraphicBase::SetRect(pRect);

	int l = this->m_pRect->Width() / 2;
	int s = this->m_pRect->Width() / 4;
	int os = 4;

	this->m_rc[0].SetRect(this->m_pRect->left + 16, 
						  this->m_pRect->top - os, 
						  this->m_pRect->left + 16 + 32,
						  this->m_pRect->bottom - os);

	this->m_rc[1].SetRect(this->m_rc[0].right + 48, 
						  this->m_pRect->top - os, 
						  this->m_rc[0].right + 48 + 64,
						  this->m_pRect->bottom - os);

	this->m_rc[2].SetRect(this->m_rc[1].right + 48, 
						  this->m_pRect->top - os, 
						  this->m_rc[1].right + 48 + 32,
						  this->m_pRect->bottom - os);

	this->m_rc[3].SetRect(this->m_rc[2].right + 32,
						  this->m_pRect->top - os, 
						  this->m_rc[2].right + 48 + 32,
						  this->m_pRect->bottom - os);
}

void UI_Menu::SetID(int id)
{
	GraphicBase::SetID(id);
}

void UI_Menu::SetBaseWindow(CWnd *pWnd)
{
	GraphicBase::SetBaseWindow(pWnd);
}